
package com.monlyu.bo;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.monlyu.bean.Page;
import com.monlyu.bean.Pages;
import com.monlyu.util.DateUtil;
import com.monlyu.util.StringUtil;
import com.monlyu.util.WebOptUtil;
import com.monlyu.vo.Message;

/********************************************************************************
 * <b>功能描述☞</b><br>
 * <b>开发公司☞</b>重庆唯尼科技有限公司<br>
 * <b>版权☞</b>本文件归重庆唯尼科技有限公司所有<br>
 * <b>项目名☞</b>NewsSys<br>
 * <b>文件路径☞</b>com.monlyu.bo.MessageBo.java<br>
 * <b>开发日期☞</b>2010-2-24 <br>
 * 
 * @author zengjun
 * @since JDK 5
 * @version 1.0.8
 ********************************************************************************/
@Repository(value = "messageBo")
public class MessageBo extends BaseBo {

    public Pages<Message> getAllMsgs(Page page){
        Criteria criteria = dao.initCriteria(Message.class);
        criteria.add(Restrictions.between(
            "createDate",
            DateUtil.getStartTimeOfDate(DateUtil.now()),
            DateUtil.getEndTimeOfDate(DateUtil.now())));
        criteria.addOrder(Order.desc("createDate"));
        return buildPages(page, criteria);
    }

    public String insert(Message msg){
        if (StringUtil.isEmpty(msg.getContent())) {
            return "信息内容不能为空！";
        }
        msg.setCreateDate(DateUtil.now());
        msg.setCreateUser(WebOptUtil.curUser());
        dao.insertOrUpdate(msg);
        return "发送成功！";
    }

    public String deleteOldMsgs(){
        Criteria criteria = dao.initCriteria(Message.class);
        criteria.add(Restrictions.le("createDate", DateUtil.getStartTimeOfDate(DateUtil.now())));
        int size = criteria.list().size();
        dao.execute("delete from com.monlyu.vo.Message where createDate<?", DateUtil.getStartTimeOfDate(DateUtil.now()));
        return "操作成功！成功删除" + size + "条信息";
    }

}
